import { defineStore } from 'pinia'
import { createStorage } from '@/utils/Storage'
import { store } from '@/store'
import { ACCESS_TOKEN, CURRENT_USER } from '@/store/mutation-types'
import { doLogout, getUserInfo } from '@/api/system/user'
import { PageEnum } from '@/enums/pageEnum'
import router from '@/router'
import type { IUserInfo } from '@/api/system/types'

const Storage = createStorage({ storage: localStorage })

interface IUserState {
  token?: string
  userInfo: Nullable<IUserInfo>
  lastUpdateTime: number
}

// interface LoginParams {
//   username: string
//   password: string
// }

export const useUserStore = defineStore({
  id: 'app-user',
  state: (): IUserState => ({
    userInfo: null,
    token: undefined,
    lastUpdateTime: 0,
  }),
  getters: {
    getUserInfo(): IUserInfo {
      return this.userInfo || Storage.get(CURRENT_USER, '') || {}
    },
    getToken(): string {
      return this.token || Storage.get(ACCESS_TOKEN, '')
    },
    getLastUpdateTime(): number {
      return this.lastUpdateTime
    },
  },
  actions: {
    setToken(token: string | undefined) {
      this.token = token || ''
      Storage.set(ACCESS_TOKEN, token)
    },
    setUserInfo(info: IUserInfo | null) {
      if (!info) {
        return
      }
      this.userInfo = info
      this.lastUpdateTime = new Date().getTime()
      // Storage.set(CURRENT_USER, info)
    },

    // async Login(params: LoginParams) {
    //   try {
    //     const response = await login(params)
    //     const { result, code } = response
    //     if (code === ResultEnum.SUCCESS) {
    //       // save token
    //       this.setToken(result.token)
    //     }
    //     return Promise.resolve(response)
    //   }
    //   catch (error) {
    //     return Promise.reject(error)
    //   }
    // },

    async GetUserInfo(phone?: string) {
      return new Promise((resolve, reject) => {
        const p = phone || this.getUserInfo.phone
        if (!p) {
          return reject(new Error('用户信息不存在！'))
        }
        getUserInfo(p)
          .then((res) => {
            this.setUserInfo(res.data)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    async Logout() {
      if (this.getToken) {
        try {
          await doLogout()
        }
        catch {
          console.error('注销Token失败')
        }
      }
      this.setToken(undefined)
      this.setUserInfo(null)
      Storage.remove(ACCESS_TOKEN)
      Storage.remove(CURRENT_USER)
      router.push(PageEnum.BASE_LOGIN)
      location.reload()
    },
  },
})

// Need to be used outside the setup
export function useUserStoreWidthOut() {
  return useUserStore(store)
}
